package com.sg.common;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @description: 接口统一封装响应数据
 * @author: hze 浩月红
 * @time: 2022/07/23 下午11:49
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ResponseInfo<T> {
    private Integer status;
    private String errorInfo;
    private T data;

    public static final Integer SUCCESS_STATUS = 0;
    public static final Integer ERROR_STATUS = 1;

    public static  ResponseInfo<Object> UNKNOWN = error(CodeMsg.SERVER_ERROR);

    /**
     * 成功时候的调用
     * */
    public static <T> ResponseInfo<T> success(T data){
        return new ResponseInfo<>(data);
    }


    /**
     * 成功时候的调用
     * */
    public static <T> ResponseInfo<T> success(T data, String message){
        return new ResponseInfo<>(data, message);
    }

    /**
     * 失败时候的调用
     * */
    public static <T> ResponseInfo<T> error(CodeMsg cm){
        return new ResponseInfo<>(cm);
    }

    /**
     * 失败时候的调用
     * */
    public static <T> ResponseInfo<T> error(String msg){
        return new ResponseInfo<>(ERROR_STATUS,msg,null);
    }

    /**
     * 失败时候的调用
     * */
    public static <T> ResponseInfo<T> error(String msg, Integer status){
        return new ResponseInfo<>(status,msg,null);
    }



    /**
     * 失败时候的调用
     * */
    public static <T> ResponseInfo<T> error(String msg, Integer status, T data){
        return new ResponseInfo<>(status,msg,data);
    }



    private ResponseInfo(CodeMsg cm) {
        if(cm == null) {
            return;
        }
        this.status = cm.getStatus();
        this.errorInfo = cm.getMsg();
    }
    private ResponseInfo(T data) {
        this.status = SUCCESS_STATUS;
        this.errorInfo = "成功";
        this.data = data;

    }

    private ResponseInfo(T data, String message) {
        this.status = SUCCESS_STATUS;
        this.errorInfo = message;
        this.data = data;

    }

}
